
package com.lzb.randomturntable.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.basic.utils.SPUtil;
import com.lzb.randomturntable.Content;
import com.lzb.randomturntable.R;
import com.lzb.randomturntable.databinding.DialogCustomSettingBinding;
import com.lzb.randomturntable.databinding.DialogShowSettingBinding;


public class CustomSettingDialog extends BaseSmartDialog<DialogCustomSettingBinding> implements View.OnClickListener {

    private int min, max, amount;
    private boolean isOpen;

    public CustomSettingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_custom_setting;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
        mDataBinding.ivOpenRepeat.setOnClickListener(this);

        min = SPUtil.getInt(getContext(), Content.randomNumberMin, 1);
        mDataBinding.etMin.setText(min + "");

        max = SPUtil.getInt(getContext(), Content.randomNumberMax, 100);
        mDataBinding.etMax.setText(max + "");

        amount = SPUtil.getInt(getContext(), Content.randomNumberAmount, 1);
        mDataBinding.etAmount.setText(amount + "");

        isOpen = SPUtil.getBoolean(getContext(), Content.isRandomNumberRepeat, false);
        mDataBinding.ivOpenRepeat.setImageResource(isOpen ? R.drawable.on : R.drawable.off2);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            String minStr = mDataBinding.etMin.getText().toString();
            if (TextUtils.isEmpty(minStr)) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_num));
                return;
            }
            String maxStr = mDataBinding.etMax.getText().toString();
            if (TextUtils.isEmpty(maxStr)) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_num));
                return;
            }
            String amountStr = mDataBinding.etAmount.getText().toString();
            if (TextUtils.isEmpty(amountStr)) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_amount));
                return;
            }

            min = Integer.parseInt(minStr);
            max = Integer.parseInt(maxStr);
            amount = Integer.parseInt(amountStr);

            if (min > max) {
                ToastUtils.showShort(getContext().getString(R.string.scope_not_reasonable));
                return;
            }

            if (!isOpen && (max - min + 1) < amount) {
                ToastUtils.showShort(getContext().getString(R.string.amount_not_reasonable));
                return;
            }

            SPUtil.putInt(getContext(), Content.randomNumberMin, min);
            SPUtil.putInt(getContext(), Content.randomNumberMax, max);
            SPUtil.putInt(getContext(), Content.randomNumberAmount, amount);
            SPUtil.putBoolean(getContext(), Content.isRandomNumberRepeat, isOpen);
            dismiss();
            if (listener != null) {
                listener.clickConfirm();
            }
        } else if (id == R.id.ivOpenRepeat) {//允许重复
            isOpen = !isOpen;
            mDataBinding.ivOpenRepeat.setImageResource(isOpen ? R.drawable.on : R.drawable.off2);
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }
}
